package com.rizzo.android.ui.adapter;

import android.content.Context;
import android.graphics.drawable.TransitionDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.rizzo.android.R;

/**
 * Created with IntelliJ IDEA.
 * User: jalie
 * Date: 12/28/12
 * Time: 11:56 AM
 * To change this template use File | Settings | File Templates.
 */
public class ContentViewAdapter extends BaseAdapter {

    private static final int TWO_COLUMNS = 0;
    private static final int THREE_COLUMNS = 1;

    private int lastposition;
    private boolean backgroundTransitioned;

    private Context context;
    private ListView listView;

    public ContentViewAdapter(Context context, ListView listView) {
        this.context = context;
        this.listView = listView;
    }

    @Override
    public int getItemViewType(int position) {
        return position % 2;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getCount() {
        return 100;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        switch (getItemViewType(position)) {
            case TWO_COLUMNS:
                if (convertView == null) {
                    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater.inflate(R.layout.two_column_content_list_row, null);
                    ViewHolder holder = new ViewHolder();
                    holder.textOne = (TextView) convertView.findViewById(R.id.txt_view_one);
                    holder.textTwo = (TextView) convertView.findViewById(R.id.txt_view_two);
                    updateTwoColumns(position, holder);
                    convertView.setTag(holder);
                } else {
                    ViewHolder holder = (ViewHolder) convertView.getTag();
                    updateTwoColumns(position, holder);
                }
                break;
            case THREE_COLUMNS:
                if (convertView == null) {
                    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater.inflate(R.layout.three_column_content_list_row, null);
                    ViewHolder holder = new ViewHolder();
                    holder.textOne = (TextView) convertView.findViewById(R.id.txt_view_one);
                    holder.textTwo = (TextView) convertView.findViewById(R.id.txt_view_two);
                    holder.textThree = (TextView) convertView.findViewById(R.id.txt_view_three);

                    updateThreeColumns(position, holder);
                    convertView.setTag(holder);
                } else {
                    ViewHolder holder = (ViewHolder) convertView.getTag();
                    updateThreeColumns(position, holder);
                }
                break;
            default:
                Log.e(this.getClass().getCanonicalName(), "Weird unexpected itemViewType encountered...");
                break;
        }

        Animation animation;
        if (position >= lastposition) {
            animation = new TranslateAnimation(0f, 0f, 100f, 0f);
            animation.setFillAfter(true);
            animation.setDuration(700);
            convertView.startAnimation(animation);
            if (position >= 50 && !backgroundTransitioned) {
                TransitionDrawable transition = (TransitionDrawable) listView.getBackground();
                transition.startTransition(700);
                backgroundTransitioned = true;
            }
        } else {
            if (position < 50 && backgroundTransitioned) {
                TransitionDrawable transition = (TransitionDrawable) listView.getBackground();
                transition.reverseTransition(700);
                backgroundTransitioned = false;
            }
        }

        lastposition = position;

        return convertView;
    }

    private void updateThreeColumns(final int position, ViewHolder holder) {
        holder.textOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast toast = Toast.makeText(context, "Clicked position: " + String.valueOf(position) + " - one", Toast.LENGTH_SHORT);
                toast.show();
            }
        });
        holder.textOne.setText(String.valueOf(position) + " one");
        holder.textTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast toast = Toast.makeText(context, "Clicked position: " + String.valueOf(position) + " - two", Toast.LENGTH_SHORT);
                toast.show();
            }
        });
        holder.textTwo.setText(String.valueOf(position) + " two");
        holder.textThree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast toast = Toast.makeText(context, "Clicked position: " + String.valueOf(position) + " - three", Toast.LENGTH_SHORT);
                toast.show();
            }
        });
        holder.textThree.setText(String.valueOf(position) + " three");
    }

    private void updateTwoColumns(final int position, ViewHolder holder) {
        holder.textOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast toast = Toast.makeText(context, "Clicked position: " + String.valueOf(position) + " - one", Toast.LENGTH_SHORT);
                toast.show();
            }
        });
        holder.textOne.setText(String.valueOf(position) + " one");
        holder.textTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast toast = Toast.makeText(context, "Clicked position: " + String.valueOf(position) + " - two", Toast.LENGTH_SHORT);
                toast.show();
            }
        });
        holder.textTwo.setText(String.valueOf(position) + " two");
    }

    static class ViewHolder {
        TextView textOne;
        TextView textTwo;
        TextView textThree;
    }

}
